using System;
using System.Threading.Tasks;
using U.B.DoMain;
using U.B.DoMain.工作流引擎;
using U.B.DoMain.工作流引擎.Models;
using U.B.ErrorCode;

namespace U.B.ReadApi.CommandHander.工作流CommandHander
{
    public class WorkflowCommandHandler : IWorkflow
    {
        private readonly U.B.DoMain.工作流引擎.IWorkflow _workflowEngine;
        private readonly LoginWorkflows _loginWorkflows;
        public WorkflowCommandHandler()
        {
            _workflowEngine = WorkflowFactory.CreateWorkflow("approval");
            _loginWorkflows = new LoginWorkflows(_workflowEngine);
        }

        public async Task<bool> ExecuteApprovalWorkflow(ApprovalWorkflowData data)
        {
            return await _loginWorkflows.ExecuteApprovalWorkflow(data);
        }

        public async Task<bool> StartApprovalProcess(int leaveAppId, int leaderId)
        {
            try
            {
                // 这里应该从数据库获取请假申请和审批人信息
                // 为了演示，我们创建一个模拟数据
                var data = new ApprovalWorkflowData
                {
                    LAId = leaveAppId,
                    LeaveName = "测试用户",
                    LeaveType = "年假",
                    StartTime = DateTime.Now,
                    EndTime = DateTime.Now.AddDays(1),
                    LeaveReason = "个人事务",
                    LeaderId = leaderId,
                    LeaderName = "部门领导",
                    CurrentStep = "Started",
                    LastUpdated = DateTime.Now
                };

                return await _loginWorkflows.ExecuteApprovalWorkflow(data);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"启动审批流程失败: {ex.Message}");
                return false;
            }
        }

        public async Task<bool> CompleteApproval(int appId, int result, string reason)
        {
            try
            {
                // 这里应该从数据库获取审批信息
                // 为了演示，我们创建一个模拟数据
                var data = new ApprovalWorkflowData
                {
                    LAId = 1, // 假设的请假申请ID
                    LeaveName = "测试用户",
                    LeaveType = "年假",
                    StartTime = DateTime.Now,
                    EndTime = DateTime.Now.AddDays(1),
                    LeaveReason = "个人事务",
                    LeaderId = 1, // 假设的审批人ID
                    LeaderName = "部门领导",
                    AppResult = result,
                    Reason = reason,
                    CurrentStep = "Completed",
                    LastUpdated = DateTime.Now
                };
                return await _loginWorkflows.ExecuteApprovalWorkflow(data);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"完成审批失败: {ex.Message}");
                return false;
            }
        }
    }
}